﻿using System;
using System.Collections.Generic;
using System.Reflection;
using ProjectDanmaku.Util;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;

namespace ProjectDanmaku.Data
{
    [CreateAssetMenu(fileName = "New Entity Type", menuName = "ProjectDanmaku/Entity Type", order = 0)]
    public class EntityType : ScriptableObject
    {
        public MonoScript systemScript;
        
        private Type _scriptClass;
        private object _scriptInstance;

        private bool _initOk;

        #region Script Method Groups

        private MethodInfo _instanceInitMethod;
        private MethodInfo _instanceTickMethod;
        private MethodInfo _instanceCreateInstancesMethod;

        #endregion

        private bool InitMethods()
        {
            _instanceInitMethod = ReflectionUtility.GetInstanceMethodByParamType(
                _scriptClass, "Init", typeof(object[]));

            _instanceTickMethod = ReflectionUtility.GetInstanceMethodByParamType(
                _scriptClass, "Tick", typeof(object[]));

            _instanceCreateInstancesMethod = ReflectionUtility.GetInstanceMethodByParamType(
                _scriptClass, "CreateInstances", typeof(object[]));

            return _instanceInitMethod != null && _instanceTickMethod != null && _instanceCreateInstancesMethod != null;
        }

        public void InitScript()
        {
            _scriptClass = systemScript.GetClass();
            _scriptInstance = Activator.CreateInstance(_scriptClass);
            
            InitMethods();
            
        }
    }
}
#endif